/*	
	
	Copyright (c) 2007-2009 Ryan Christiansen
	
	This software is provided 'as-is', without any express or implied
	warranty. In no event will the authors be held liable for any damages
	arising from the use of this software.
	
	Permission is granted to anyone to use this software for any purpose,
	including commercial applications, and to alter it and redistribute it
	freely, subject to the following restrictions:
	
	1. The origin of this software must not be misrepresented; you must not
	claim that you wrote the original software. If you use this software
	in a product, an acknowledgment in the product documentation would be
	appreciated but is not required.
	
	2. Altered source versions must be plainly marked as such, and must not be
	misrepresented as being the original software.
	
	3. This notice may not be removed or altered from any source
	distribution.
	
*/
package funit.core
{
	import flash.events.EventDispatcher;
	
	import funit.core.events.AsyncEvent;
	
	import sv.errors.NotImplementedError;
	import sv.reflection.Type;
	
	[Event(name="finish", type="funit.core.events.AsyncEvent")]
	
	public class TestBase extends EventDispatcher implements ITest
	{
		// invoke static framework initializers
		private static var framework:FUnitFramework = new FUnitFramework();
		
		private var _testName:TestName = null;
		private var _description:String = null;
		
		private var _fixture:Object = null;
		private var _fixtureType:Type = null;
		private var _runState:RunState = null;
		private var _ignoreReason:String = null;
		
		
		/**
		 * @param fixtureType
		 */
		public function TestBase( fixtureType:Type = null )
		{
			_testName = new TestName();
			_testName.name = (fixtureType != null) ? fixtureType.name : "";
			_testName.fullName = (fixtureType != null) ? fixtureType.fullName : "";
			
			_fixtureType = fixtureType;
			_runState = RunState.Runnable;
		}
		
		
		/**
		 * [abstract]
		 */
		public function run( listener:ITestListener ) : void
		{
			
		}
		
		/**
		 * [abstract] Used to cancel any asynchronous processes and end the test.
		 */
		public function endRun() : void
		{
			
		}
		
		/**
		 * Enters asynchronous mode and begins a delay period. The test
		 * can only be marked 'complete' when one of three events occur.
		 * 
		 * <ul>
		 * 		<li>If <code>finishTest()</code> is called before the timeout expires.</li>
		 *		<li>If any <code>Error</code> is thrown during the delay period.</li>
		 *		<li>If the timeout expires and neither of the above has occured.</li>
		 * </ul>
		 * 
		 * @param timeout The time milliseconds when any pending asynchronous
		 * routines will expire.
		 * 
		 * @see #finishTest()
		 */
		protected function delayTestFinish( timeout:int ) : void
		{
			throw new NotImplementedError();
		}
		
		/**
		 * Marks the test as complete and closes asynchronous mode successfully.
		 * @param result
		 */
		protected function finishTest( result:TestResult ) : void
		{
			dispatchEvent( new AsyncEvent(AsyncEvent.FINISH, result) );
		}
		
		/**
		 * @return
		 */
		protected function createTestResult() : TestResult
		{
			return new TestResult(this, this.testName.fullName);
		}
		
		
		/**
		 * 
		 */
		public function get testName() : TestName
		{
			return _testName;
		}
		
		
		/**
		 * 
		 */
		public function get description() : String
		{
			return _description;
		}
		
		public function set description( value:String ) : void
		{
			_description = value;
		}
		
		
		/**
		 * 
		 */
		public function get ignoreReason() : String
		{
			return _ignoreReason;
		}
		
		public function set ignoreReason( value:String ) : void
		{
			_ignoreReason = value;
		}
		
		
		/**
		 * 
		 */
		public function get fixture() : Object
		{
			return _fixture;
		}
		
		public function set fixture( value:Object ) : void
		{
			_fixture = value;
		}
		
		
		/**
		 * 
		 */
		public function get fixtureType() : Type
		{
			return _fixtureType;
		}
		
		
		/**
		 * 
		 */
		public function get runState() : RunState
		{
			return _runState;
		}
		
		public function set runState( value:RunState ) : void
		{
			_runState = value;
		}
		
		
		/**
		 * [abstract]
		 */
		public function get testType() : String
		{
			return null;
		}
		
		
		/**
		 * [abstract]
		 */
		public function get isSuite() : Boolean
		{
			return false;
		}
		
		
		/**
		 * [abstract]
		 */
		public function get testCount() : int
		{
			return 0;
		}
		
		
		/**
		 * [abstract]
		 */
		public function get tests() : Array
		{
			return null;
		}
		
	}
	
}
